package receive;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import util.BookMarketUtil;

public class BookMarkReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();

        String message = bundle.getString("message");
        Log.d("BookMarkReceiver","message111111111111111="+message);
        if (!TextUtils.isEmpty(message)){
            JSONObject jsonObject= null;
            try {
                jsonObject = new JSONObject(message);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            if (jsonObject!=null) {
                String mark = jsonObject.optString("mark");
                if ("addwebsite".equalsIgnoreCase(mark)){
                    String name=jsonObject.optString("name");
                    int id=jsonObject.optInt("id");
                    String url=jsonObject.optString("url");
                    BookMarketUtil.insertBookMark(context,id,name,url);
                }
                if ("delwebsite".equalsIgnoreCase(mark)){

                    int id=jsonObject.optInt("id");

                    BookMarketUtil.deleteBookMark(context,id);
                }
                if ("updatewebsite".equalsIgnoreCase(mark)){
                    int id=jsonObject.optInt("id");
                    String url =jsonObject.optString("url");
                    String name=jsonObject.optString("name");
                    BookMarketUtil.updateBookMark(context,id,url,name);
                }
            }
        }
    }
}
